package com.example.demo.collerct;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Blog;
import com.example.demo.entity.Type;
import com.example.demo.entity.User;
import com.example.demo.service.Blogservice;
import com.example.demo.service.Typeservice;
import com.example.demo.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.net.idn.Punycode;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class Usercollerct {
    @Autowired
    Userservice userservice;
    @Autowired
    Blogservice blogservice;
    @Autowired
    Typeservice typeservice;

@RequestMapping(value = "/Denlv")//登录
    public String denlv(String username, String password, Model model, HttpSession session){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        User user = userservice.selectA(queryWrapper);
        if (user!=null){
            session.setAttribute("user",user);
        return "forward:a";
        }else {
            model.addAttribute("msg","失敗");
            return "admin/login";
        }
    }
    @RequestMapping("/a")
    public  String aa(){
    return "admin/index";
    }
    @RequestMapping("/blogsa")
    public String Blaaogs(Model model,@RequestParam(defaultValue = "1") Integer pageIndex,String title,Integer typeId){
    QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        Page<Blog> page = new Page<>(pageIndex,3);
        List<Type> types = typeservice.selectAC();
        for (Type type : types) {
            System.out.println(type);

        }
        model.addAttribute("aka",types);
        model.addAttribute("page", blogservice.selectAka(page,title,typeId));
        model.addAttribute("title",title);
        model.addAttribute("typeId",typeId);
        model.addAttribute("pageIndex",pageIndex);
        return "admin/blogs";

    }
    @RequestMapping("/types")
public String l(){
    return "admin/types";
    }

    @RequestMapping("/tags")
    public String la(){
        return "admin/tags";
    }
}
